package com.yxcz.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yxcz.model.Document;
import com.yxcz.service.DocumentService;
import com.yxcz.service.LoginService;

@Controller
@RequestMapping("document")
public class DocumentController {

	@Resource
	private DocumentService documentService;
	@Resource
	private LoginService loginService;

	@RequestMapping(value = "index")
	public ModelAndView index(ModelAndView modelAndView) {
		modelAndView.setViewName("document/index");
		return modelAndView;
	}

	@RequestMapping(value = "view")
	public ModelAndView view(Integer id, ModelAndView modelAndView) {
		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		modelAndView.addObject("document", documentService.get(id));
		modelAndView.setViewName("document/view");
		return modelAndView;
	}
	
	@RequestMapping(value = "edit")
	public ModelAndView edit(Integer id, ModelAndView modelAndView,HttpSession session) {
		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		
		Document document =documentService.get(id);
		
		if(document.getUser()==null||loginService.getUserInfo(session.getAttribute("accountName").toString()).getId()!=document.getUser().getId())
		{
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		modelAndView.addObject("document", document);
		modelAndView.setViewName("document/edit");
		return modelAndView;
	}

	@RequestMapping(value = "manager")
	public ModelAndView manager(ModelAndView modelAndView) {

		modelAndView.addObject("documents", documentService.list());
		modelAndView.setViewName("document/manager");
		return modelAndView;
	}

	@RequestMapping(value = "addDocument")
	public ModelAndView index(String name, String content, ModelAndView modelAndView, HttpSession session) {

		try {
			documentService.add(content, name,
					loginService.getUserInfo(session.getAttribute("accountName").toString()).getId());
		} catch (Exception e) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		modelAndView.addObject("msg", "修改");
		modelAndView.setViewName("Common/success");
		return modelAndView;
	}

	@RequestMapping(value = "delete")
	public ModelAndView index(Integer id, ModelAndView modelAndView) {
		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		documentService.delete(id);
		modelAndView.addObject("msg", "删除");
		modelAndView.setViewName("Common/success");
		return modelAndView;
	}
	
	@RequestMapping(value = "updateDocument")
	public ModelAndView index(Document doc, ModelAndView modelAndView) {
		
		documentService.update(doc);
		modelAndView.addObject("msg", "保存");
		modelAndView.setViewName("Common/success");
		return modelAndView;
	}
}
